package io.renren.modules.sys.controller;

import java.util.HashMap;
import java.util.List;

import io.renren.common.utils.Constant;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxCourseTypeEntity;
import io.renren.modules.sys.service.JxCourseTypeService;
import io.renren.common.utils.R;



/**
 * 课程
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-11 09:28:47
 */
@RestController
@RequestMapping("sys/jxcoursetype")
public class JxCourseTypeController extends AbstractController {
    @Autowired
    private JxCourseTypeService jxCourseTypeService;



    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxcoursetype:list")
    public List<JxCourseTypeEntity> list(){
        List<JxCourseTypeEntity> jxCourseList = jxCourseTypeService.queryList(new HashMap<String, Object>());

        return jxCourseList;
    }

    /**
     * 选择课程(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:jxcoursetype:select")
    public R select(){
        List<JxCourseTypeEntity> jxCourseList = jxCourseTypeService.queryList(new HashMap<String, Object>());

        //添加一级部门
        if(getUserId() == Constant.SUPER_ADMIN){
            JxCourseTypeEntity root = new JxCourseTypeEntity();
            root.setId(0L);
            root.setName("一级课程");
            root.setParentId(-1L);
            root.setOpen(true);
            root.setLevel(0);
            jxCourseList.add(root);
        }

        return R.ok().put("jxCourseList", jxCourseList);
    }

    /**
     * 上级课程Id(管理员则为0)
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:jxcoursetype:list")
    public R info(){
        long id = 0;
        if(getUserId() != Constant.SUPER_ADMIN){
            List<JxCourseTypeEntity> jxcourseList = jxCourseTypeService.queryList(new HashMap<String, Object>());
            Long parentId = null;
            for(JxCourseTypeEntity jxcourseTypeEntity : jxcourseList){
                if(parentId == null){
                    parentId = jxcourseTypeEntity.getParentId();
                    continue;
                }

                if(parentId > jxcourseTypeEntity.getParentId().longValue()){
                    parentId = jxcourseTypeEntity.getParentId();
                }
            }
            id = parentId;
        }

        return R.ok().put("id", id);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxcoursetype:info")
    public R info(@PathVariable("id") Long id){
        JxCourseTypeEntity jxCourse = jxCourseTypeService.getById(id);

        return R.ok().put("jxCourseType", jxCourse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:jxcoursetype:save")
    public R save(@RequestBody JxCourseTypeEntity jxCourse){
        ValidatorUtils.validateEntity(jxCourse,AddGroup.class);
        jxCourseTypeService.save(jxCourse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:jxcoursetype:update")
    public R update(@RequestBody JxCourseTypeEntity jxCourse){
        ValidatorUtils.validateEntity(jxCourse,UpdateGroup.class);
        jxCourseTypeService.updateById(jxCourse);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxcoursetype:delete")
    public R delete(long id ){
        //判断是否有子部门
        List<Long> jxCourseIdList  = jxCourseTypeService.queryJxCourseIdList(id);
        if(jxCourseIdList.size() > 0){
            return R.error("请先删除子课程");
        }

        jxCourseTypeService.removeById(id);

        return R.ok();
    }

}
